﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using KaiXin001er.Objects;
namespace KaiXin001er.UTIL
{
    public class ConfigManager
    {
        private static string _DataFilePath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments) + @"\Kaixin001PlayerData\";

        private static List<Account> _AcctList;
        private static bool dirExist = MakeDataDir();
        public static List<Account> AcctList
        {
            get
            {
                if (_AcctList == null) { _AcctList = new List<Account>(); ReadAcctConfigs(); }
                return ConfigManager._AcctList;
            }

        }
        private static ProgramConfiger _ProgramConfig;

        public static ProgramConfiger ProgramConfig
        {
            get
            {
                if (_ProgramConfig == null) ReadProgramconfig();
                return ConfigManager._ProgramConfig;
            }

        }

        private static bool MakeDataDir()
        {

            //默认data目录必须存在
            DirectoryInfo Dinfo = new DirectoryInfo(_DataFilePath);
            if (!Dinfo.Exists) Dinfo.Create();
            return true;
        }

        private static void ReadAcctConfigs()
        {
            try
            {
                
                DirectoryInfo di = new DirectoryInfo(_DataFilePath);
                foreach (FileInfo fi in di.GetFiles("UserConfig*.*"))
                {
                    BinaryFormatter formater = new BinaryFormatter();
                    Stream stm = new FileStream(fi.FullName, FileMode.Open);
                    object o = formater.Deserialize(stm);
                    _AcctList.Add((Account)o);
                    stm.Close();
                }
            }
            catch (Exception)
            {
            }


        }

        private static void ReadProgramconfig()
        {
            MakeDataDir();
            BinaryFormatter formater = new BinaryFormatter();
            try
            {
                FileInfo fi = new FileInfo(_DataFilePath + @"ProgramConfig.bin");
                if (fi.Exists)
                {
                    Stream stm = new FileStream(fi.FullName, FileMode.Open);
                    object o = formater.Deserialize(stm);
                    _ProgramConfig = (ProgramConfiger)o;
                    stm.Close();
                }//如果没有就产生一个空的吧
                else
                {
                    _ProgramConfig = new ProgramConfiger();
                }
            }
            catch (Exception)
            {
                _ProgramConfig = new ProgramConfiger();
            }

        }
        public static void SaveAcctConfig()
        {//删除原有的用户config
            DirectoryInfo di = new DirectoryInfo(_DataFilePath);

            foreach (FileInfo fi in di.GetFiles("UserConfig*.*"))
            {
                fi.Delete();
            }
            //新建后来的用户config
            if (_AcctList == null) return;
            foreach (Account acct in _AcctList)
            {
                SaveConfig(acct, "UserConfig" + Guid.NewGuid().ToString() + ".bin");
            }
        }
        public static void SaveProgramConfig()
        {
            SaveConfig(_ProgramConfig, "ProgramConfig.bin");
        }
        private static void SaveConfig(Object configer, string fileName)
        {
            if (MakeDataDir())
            {
                BinaryFormatter formater = new BinaryFormatter();
                Stream stm = new FileStream(_DataFilePath  + fileName, FileMode.Create);
                
                formater.Serialize(stm, configer);
                stm.Flush();
                stm.Close();
                
            }
        }
    }
    public class AcctComparer : IComparer<Account>
    {

        #region IComparer<Acct> 成员

        public int Compare(Account x, Account y)
        {
            if (x.StartSeq > y.StartSeq) return 1;
            if (x.StartSeq == y.StartSeq) return 0;
            
            return -1;
        }

        #endregion
    }
}
